package me.moscrop.RootCommand.commands;

import me.moscrop.RootCommand.utilities.Utility;

import org.bukkit.Location;
import org.bukkit.TreeType;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.ChatColor;

public class Tree {
	
	public enum species
	{
		BIRCH, REDWOOD, TREE, NULLVALUE;

		private static species available(String str)
	    {
	        try {
	            return valueOf(str.toUpperCase());
	        } 
	        catch (Exception ex) {
	            return NULLVALUE;
	        }
	    }   
	}

	public static Boolean generateTree(CommandSender sender, Command command, String label, String[] args) {
		if (!(sender instanceof Player)) {
			sender.sendMessage(" Command must be run in game.");
			return true;
		}
		
		String treeSpecies;
		boolean largeTree;
		Player player = (Player) sender;
		
		if (args.length == 0) {
			return false;
		} else if(args.length == 1) {
			treeSpecies = args[0];
			largeTree = false;
		} else if(args.length == 2 && (args[1].contentEquals("big"))) {
			treeSpecies = args[0];
			largeTree = true;
		} else {
			return false;
		}
				
		TreeType selectedTree = getTreeType(treeSpecies, largeTree);
		Block block = player.getTargetBlock(null, 50);
				
		if(block != null) {
			
			Location location = block.getLocation();
			final boolean success = player.getWorld().generateTree(Utility.getSafeLocationAt(location), selectedTree);

			if (success) {
			        player.sendMessage( ChatColor.GREEN + "Tree Grown" );
			        return true;
			} else {
			        player.sendMessage( ChatColor.RED + "An Error Occured" );
			}
		} 
		return true;
	}


	public static TreeType getTreeType(String treeSpecies, boolean largeTree) {
		switch (species.available(treeSpecies)) {
		
		case BIRCH:
			return TreeType.BIRCH;

		case REDWOOD:
			return (largeTree) ? TreeType.TALL_REDWOOD : TreeType.REDWOOD;
			
		case TREE:
			return (largeTree) ? TreeType.BIG_TREE : TreeType.TREE; 

		default:
			return null;
			
		}			
	}
	
	
}
